package com.yunfei.api.v1;
import com.google.gson.Gson;
import com.yunfei.api.v1.magerIndcInfo.param.FaRenInfoManagerParam;
import com.yunfei.api.v1.magerIndcInfo.param.TemplateManagerParam;
import com.yunfei.api.v1.magerIndcInfo.service.MagerIndcInfoService;
import com.yunfei.api.v2.contants.QueryHeTongSignInfoContant;
import com.yunfei.api.v2.param.CreatePersonInfoParam;
import com.yunfei.api.v2.param.LongTokenParam;
import com.yunfei.api.v2.service.*;
import com.yunfei.bo.result.ResultModel;
import com.yunfei.enums.ResultStatus;
import com.yunfei.fegin.RedisFeignService;
import com.yunfei.mapper.aiiidb.*;
import com.yunfei.mapper.crm.TunRegisterMapper;
import com.yunfei.ribbon.RedisServiceRibbon;
import com.yunfei.service.RedisService;
import com.yunfei.service.ValidMobileAndCodeService;
import com.yunfei.utils.TokenUtil;
import com.yunfei.utils.http.HttpIOUtil;
import com.yunfei.utils.string.StringUtil;
import com.yunfei.yunhetong.upload.result.YunHeTongUploadImageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSort;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description: 法人信息管理
 * @Author: zhengyunfei
 * @Date: 2018/6/15
 * @time:11:26
 */
@SuppressWarnings("ALL")
@RestController
@RequestMapping("/api/v1/user")
@Api(value = QueryHeTongSignInfoContant.PERSON_NAMESPACE,tags =  QueryHeTongSignInfoContant.PERSON_NAMESPACE )
public class UserApiController {
    @Autowired
    private TPersonalUserMapper tPersonalUserMapper;
    @Autowired
    private GetStatusMapper getStatusMapper;
    @Autowired
    private RedisFeignService redisFeignService;
    @Autowired
    private RedisService redisService;
    @Autowired
    private RedisServiceRibbon redisServiceRibbon;
    @Autowired
    private ValidMobileAndCodeService validMobileAndCodeService;
    @Autowired
    private Environment env;
    @Autowired
    private MagerIndcInfoMapper magerIndcInfoMapper;
    @Autowired
    private MagerIndcInfoTemplateMapper magerIndcInfoTemplateMapper;
    @Autowired
    private MagerIndcInfoService magerIndcInfoService;
    @Autowired
    private HTSGetStatusService htsGetStatusService;
    @Autowired
    private HTSGetAccountInfoService htsGetAccountInfoService;
    @Autowired
    private HTSAddAccountService htsAddAccountService;
    @Autowired
    private HTYuYueInfoService htYuYueInfoService;
    @Autowired
    private GetElecHeTongNoService getElecHeTongNoService;
    @Autowired
    private TunRegisterMapper tunRegisterMapper;
    @Autowired
    private CreatePersonInfoService createPersonInfoService;
    @Autowired
    private ElecContCustAddInfoMapper elecContCustAddInfoMapper;
    @Autowired
    private FaRenInfoMapper faRenInfoMapper;
    @Autowired
    private GetLongTokenService getLongTokenService;

    /**
     * @Description: 更新或添加法人信息
     * @Author: zhengyunfei
     * @Date: 2019/7/29
     * @time:10:30
     */
    @PostMapping(value="/faren/update")
    @ApiOperationSort(value = 3)
    @ApiOperation(position = 3,value = QueryHeTongSignInfoContant.UPDATE_FAREN_INFO,notes = QueryHeTongSignInfoContant.UPDATE_FAREN_INFO,response = ResultModel.class)
    @ResponseBody
    public Object createPerson(FaRenInfoManagerParam param,String token){
        //更新法人信息
        ResultModel resultModel=new ResultModel();
        try{
            String localPath=param.getLocalPath();//本地路径
            String caCustId=param.getAgentUserId();
            String fileId=param.getFileId();//调用云合同接口返回的印章id
            if(StringUtil.isNullOrEmpty(fileId)){
                //只有在上传的时候，新增的时候，才需要调用
                if(!StringUtil.isNullOrEmpty(localPath)&&!StringUtil.isNullOrEmpty(caCustId)){
                    //管理人公章，法定代表人章
                    HttpIOUtil httpReuqest = new HttpIOUtil();
                    String uploadResult="";
                    Gson gson=new Gson();
                    uploadResult=httpReuqest.uploadImageToYunHeTong(token,localPath,caCustId);
                    YunHeTongUploadImageResult yunHeTongUploadResult=gson.fromJson(uploadResult, YunHeTongUploadImageResult.class);
                    if(yunHeTongUploadResult.getCode()!=200){
                        resultModel.setCode(yunHeTongUploadResult.getCode());
                        resultModel.setMessage(yunHeTongUploadResult.getMsg());
                        return resultModel;
                    }else{
                        //印章id
                        TemplateManagerParam templateManagerParam=new TemplateManagerParam();
                        templateManagerParam.setType(param.getType());
                        templateManagerParam.setCaCustId(caCustId);
                        templateManagerParam.setOperatorId(param.getOperatorId());
                        String moulageId=yunHeTongUploadResult.getData().getMoulageId();
                        templateManagerParam.setFileId(moulageId);
                        magerIndcInfoService.add(templateManagerParam);
                    }
                }
            }
            magerIndcInfoTemplateMapper.updateFaRenInfo(param);
            resultModel.setCode(ResultStatus.SUCCESS.getCode());
            resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
            return resultModel;
        }catch (Exception e){
            e.printStackTrace();
            resultModel.setCode(ResultStatus.ERROR.getCode());
            resultModel.setMessage(ResultStatus.ERROR.getMessage());
            e.printStackTrace();
        }
        return resultModel;
    }

    /**
     * @Description: 创建个人用户
     * @Author: zhengyunfei
     * @Date: 2019/7/29
     * @time:10:30
     */
    @PostMapping(value="/create")
    @ApiOperationSort(value = 3)
    @ApiOperation(position = 3,value = QueryHeTongSignInfoContant.ADD_PERSON,notes = QueryHeTongSignInfoContant.ADD_PERSON,response = ResultModel.class)
    @ResponseBody
    public Object createPerson(CreatePersonInfoParam param){
        //填写完预约信息，需要调用创建个人客户接口
        ResultModel resultModel=null;
        try{
            param.setCaType("B2");//证书类型
            param.setIdentityRegion("0");//大陆
            param.setPhoneRegion("0");//大陆
            String certifyType=param.getCertifyType();
            if(!StringUtil.isNullOrEmpty(certifyType)&&!"a".equals(certifyType)){
                //除了大陆身份证，统一传  z  就可以了
                param.setCertifyType("z");
            }
            resultModel=createPersonInfoService.createPersonInfo(param);
        }catch (Exception e){
            e.printStackTrace();
        }
        return resultModel;
    }

}
